//
// Created by yangchao on 2022/5/5.
// 46. 全排列: https://leetcode-cn.com/problems/permutations/
//

#include <vector>
#include <cstring>
using namespace std;

class Permute{
public:
    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int>> res;
        vector<int> path;
        bool visited[nums.size()];
        memset(visited, false,sizeof(visited));
        backtrack(0,visited,path,res,nums);
        return res;
    }
    void backtrack(int index, bool visited[],vector<int>& path,vector<vector<int>>& res,vector<int>& nums) {
        if (index == nums.size()) {
            res.push_back(path);
            return;
        }
        for (int i = 0; i < nums.size(); ++i) {
            if (!visited[i]) {
                visited[i] = true;
                path.push_back(nums[i]);
                backtrack(index+1,visited,path,res,nums);
                path.pop_back();
                visited[i] = false;
            }
        }
    }
};

